import {
  createApp
} from "vue";
import router from "./router/router";
import store from "../src/store/index";
import './styles/common.scss'
import ElementPlus, {ElMessage} from "element-plus";
import zhCn from "element-plus/es/locale/lang/zh-cn";
// 引入注册自定义指令
import directives from "./instruction/index.js";
import "element-plus/dist/index.css";
import App from "./App.vue";
// import "./mock/index.js";
import * as ELIcons from "@element-plus/icons-vue";

import VMdEditor from "@kangc/v-md-editor";
import vuepressTheme from "@kangc/v-md-editor/lib/theme/vuepress.js";
import "@kangc/v-md-editor/lib/style/base-editor.css";
import "@kangc/v-md-editor/lib/theme/style/vuepress.css";
import axios from "axios";
import {useRouter} from "vue-router";
import service from "@/api/request";

VMdEditor.use(vuepressTheme);

const app = createApp(App);

// 小图标引用
for (const iconName in ELIcons) {
  // 注册组件
  app.component(iconName, ELIcons[iconName]);
}

app.use(router).use(store).use(directives).use(VMdEditor).use(ElementPlus, {
  locale: zhCn,
});

app.mount("#app");


// Request interceptors
axios.interceptors.request.use(
    (config) => {
      if (sessionStorage.getItem('token'))
        config.headers.token = sessionStorage.getItem('token')
      return config;
    },
    (error) => {
      Promise.reject(error);
    }
);
// Response interceptors
axios.interceptors.response.use(
    (response) => {
      // 未认证处理
      if(response.data.code == 401){
        ElMessage.error('请先登录')
        router.push('/login')
        return;
      }
      return response.data;
    },
    (error) => {
      // do something
      return Promise.reject(error);
    }
);